#include "proteindatanode.h"
#include "proteinattribwidget.h"
#include "../renderer/renderplugin.h"

using namespace PBVP;

ProteinDataNode::ProteinDataNode(PBVP::ProteinData *pProtein, MainInterface *mainInterface)
{
	m_pProtein = pProtein;
	m_pAttribWidget = new ProteinAttribWidget(this, NULL);
	m_pRenderer = NULL;
	QVector<Atom> atoms = m_pProtein->getAtoms();
	Vector3 minExt, maxExt;
	minExt = maxExt = atoms[0].position;
	for(int i = 1; i < atoms.size(); i++) {
		if(minExt.x > atoms[i].position.x) minExt.x = atoms[i].position.x;
		if(minExt.y > atoms[i].position.y) minExt.y = atoms[i].position.y;
		if(minExt.z > atoms[i].position.z) minExt.z = atoms[i].position.z;
		if(maxExt.x < atoms[i].position.x) maxExt.x = atoms[i].position.x;
		if(maxExt.y < atoms[i].position.y) maxExt.y = atoms[i].position.y;
		if(maxExt.z < atoms[i].position.z) maxExt.z = atoms[i].position.z;
	}
	minExt[0] -= 3; minExt[1] -= 3; minExt[2] -= 3;
	maxExt[0] += 3; maxExt[1] += 3; maxExt[2] += 3;
	m_BBox.setMinimum(minExt);
	m_BBox.setMaximum(maxExt);
}

ProteinDataNode::~ProteinDataNode()
{
	delete m_pAttribWidget;
	if(m_pRenderer) {
		delete m_pRenderer;
	}
}

void ProteinDataNode::render()
{
	if(!m_pRenderer) return;
	m_pRenderer->begin();
	m_pRenderer->render(this);
	m_pRenderer->end();
}

QWidget* ProteinDataNode::getPropertyWidget() const 
{
	return m_pAttribWidget;
}

void ProteinDataNode::setRenderer(PBVP::RenderPlugin *pRenderer)
{
	DataNode::setRenderer(pRenderer);
	if(pRenderer && pRenderer->getRenderControl()) {
		m_pAttribWidget->setRenderingProperty(pRenderer->getRenderControl());
	}
}